<template>
  <svg :class="svgClass" aria-hidden="true" :style="style">
    <use :xlink:href="iconName" />
  </svg>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  name: {
    type: String,
    required: true
  },
  className: {
    type: String,
    default: ''
  },
  color: {
    type: String,
    default: ''
  },
  size: {
    type: [Number, String],
    default: undefined
  }
})

const iconName = computed(() => `#icon-${props.name}`)
const svgClass = computed(() => `svg-icon ${props.name}`)

const style = computed(() => {
  const styles = {}

  // 只有当 size 有值时才设置宽高
  if (props.size !== undefined) {
    styles.width = typeof props.size === 'number' ? `${props.size}px` : props.size
    styles.height = typeof props.size === 'number' ? `${props.size}px` : props.size
  }

  // 只有当 color 有值时才设置 fill
  if (props.color) {
    styles.fill = props.color
  } else {
    styles.fill = 'currentColor'
  }

  return styles
})
</script>

<style scoped>
.svg-icon {
  width: 1em;
  height: 1em;
  fill: currentColor;
  vertical-align: middle;
  overflow: hidden;
}
</style>
